package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.manor.ManorGuestData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/5/19 11:19
 */
public class UserManorGuestDataDaoImpl extends TempDataObjectDao<ManorGuestData> {
    public static String insertSql = "insert into t_u_manor_guest(userId,id,skinIdList,skinId,extendParam) values(?,?,?,?,?)";
    @Override
    protected boolean addData(ManorGuestData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getId()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getSkinIdList(), ",")));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSkinId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getExtendParam()));
        return getDb().executeNoneQuery(insertSql, para) > 0;
    }

    public static String updateSql = "update t_u_manor_guest set skinIdList=?,skinId=?,extendParam=? where userId=? and id = ?";
    @Override
    protected boolean updateData(ManorGuestData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getSkinIdList(), ",")));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSkinId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getExtendParam()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getId()));
        return getDb().executeNoneQuery(updateSql, para) > 0;
    }

    @Override
    protected ManorGuestData parseData(ResultSet rs) throws SQLException {
        ManorGuestData data = new ManorGuestData();
        data.setId(rs.getInt("id"));
        data.setUserId(rs.getLong("userId"));
        data.setSkinIdList(StringUtils.stringToIntegerList(rs.getString("skinIdList"), ","));
        data.setSkinId(rs.getInt("skinId"));
        data.setExtendParam(rs.getString("extendParam"));
        return data;
    }

    public static String getSql = "select * from t_u_manor_guest where userId=?";

    public Map<Integer, ManorGuestData> getGuestDataMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, ManorGuestData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                ManorGuestData guestData = parse(rs);
                map.put(guestData.getId(), guestData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }
}
